package ICM.client;

import javax.swing.*;

import ocsf.client.ObservableClient;

import common.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

/**
 * Create Client window
 */
public class ClientUI extends JFrame implements Observer {
	
	public static final int LOGIN_SCREEN = 1;
	public static final int SETTINGS_SCREEN = 2;
	public static final int Treatment_Screen = 7;
	public static final int Report_Screen = 8;
	public static final int ChangeRequest_Screen = 9 ;
	public static final int Monitoring_Screen = 10;
	public static final int Main_Screen = 11;
	public static final int SystemEngineerWindow_Screen = 12;	
	public static final int Committee_Screen = 13;
	public static final int Appointment_Screen = 14;
	public static final int EmployeesInfo_Screen = 15;
	public static final int RequestsInfo_screen = 16;
	public static final int StatisticInfo_screen = 17;
	public static final int Assessor_Screen = 18;
	public static final int Chairman_Screen = 19;
	public static final int Execution_Screen = 20;
	public static final int Supervisor_Screen = 21;
	public static final int Testing_Screen = 22;
	public static int count=0;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int currentScreen = 0;
	private Client client = null;
	private User user = null;
	/**
	 * Constructor
	 */
	public ClientUI(final Client c) {
		client = c;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("ICM Client");
		
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		JMenuItem exitMenu = new JMenuItem("Exit");
		exitMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		fileMenu.add(exitMenu);
		
		
		JMenu optionsMenu = new JMenu("Options");
		menuBar.add(optionsMenu);
		
		JMenuItem settingsMenu = new JMenuItem("Settings");
		settingsMenu.addActionListener(new ActionListener() {
																public void actionPerformed(ActionEvent arg0) {
																	try {
																		SetContent(SETTINGS_SCREEN);
																	} catch (Exception e) {
																		// TODO Auto-generated catch block
																		e.printStackTrace();
																	}
																}
															});
		optionsMenu.add(settingsMenu);
		
		setJMenuBar(menuBar);
		
		pack();
		setSize(1080,720);
		
		addWindowListener(new WindowAdapter()
	    {
	       public void windowClosing(WindowEvent e)
	       {
	    	   if (user!=null)
	    	   {
		    	    CommandMessage msg = new CommandMessage(CommandMessage.LOGOUT_USER,new Integer[] {user.getId()});
			   		// requests.
			   		try {
			   			c.sendToServer(msg);
			   		} catch (IOException e1) {
			   			// TODO Auto-generated catch block
			   			e1.printStackTrace();
			   		}
	    	   }
	            dispose();
	            System.exit(0); //calling the method is a must
	       }
	    });
	}
	
	/**
	 * Handle massage from Observable class in our case Client
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		String msg = "";
		if (arg1 instanceof TextMessage)
			msg = ((TextMessage)arg1).ToDebugMessage();
		else if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if(ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE)){
				String cmd=(String)(ms.getParams()[0]);
				if(cmd.equals(CommandMessage.LOGIN_MESSAGE))
				{
					if(ms.getParams()[1]==null)
					{
						ShowMessageBox("Error ", "Wrong credentials" ,JOptionPane.ERROR_MESSAGE);
					    count++;
					    if (count==3)
					    {
					    	ShowMessageBox("Error ", " Your entered wrong data 3 times",JOptionPane.ERROR_MESSAGE);
					    	count=0;
					    	System.exit(0);
					    }
					}
					else
					{
						ShowMessageBox("Welcome", "Login success" ,JOptionPane.INFORMATION_MESSAGE);
						user=(User)ms.getParams()[1];
						try {
							try {
								SetContent(Main_Screen);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				else if(cmd.equals("User already connected"))
				{
					ShowMessageBox("Error", "User already connected" ,JOptionPane.ERROR_MESSAGE);
				}
				else if(cmd.equals(CommandMessage.CREATE_USER))
				{
					if((Integer.parseInt(ms.getParams()[1].toString()))==1)
					{
						ShowMessageBox("Information", "User was added" ,JOptionPane.INFORMATION_MESSAGE);
						try {
							SetContent(LOGIN_SCREEN);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else if((Integer.parseInt(ms.getParams()[1].toString()))==-1)
						ShowMessageBox("Error", "User already exists" ,JOptionPane.ERROR_MESSAGE);
					else
						ShowMessageBox("Error", "Can't add user" ,JOptionPane.ERROR_MESSAGE);
				}
				else if(cmd.equals("Logout response"))
				{
					System.exit(0);
				}
			}
		}
		else {
			msg = arg1.toString();
			ShowMessageBox("Message Received", msg ,JOptionPane.INFORMATION_MESSAGE);
		}
		
	}
	
	
		
	
	

	
	/**
	 * Show pop up message
	 * @param title Message title
	 * @param message Message content
	 */
	public static void ShowMessageBox(String title, String message, int type) {
		JOptionPane.showMessageDialog(null, message, title, type);
	}
	
	public void SetContent(int screen) throws SQLException, IOException {
		if (currentScreen == screen)
			return;
		switch (screen) {
			case LOGIN_SCREEN:
				// Login screen
				setContentPane(new LogInUI(client, this));
				break;
			case SETTINGS_SCREEN:
				SettingsUI s = new SettingsUI(this);
				s.BindSettings();
				setContentPane(s);
				break;			
			case Main_Screen:
				setContentPane(new MainWindowUI(client, this));
				break;
			case Monitoring_Screen:
				setContentPane(new MonitoringRequestUI(client, this));
				break;	
			case ChangeRequest_Screen:
				setContentPane(new CreateChangeRequestUI(client, this));
				break;
			case Report_Screen:
				setContentPane(new Reports(client , this));
				break;
			case SystemEngineerWindow_Screen:
				setContentPane(new SystemEngineerUI(client , this ));
				break;
			case Treatment_Screen:
				setContentPane(new TreatmentRequestUI(client , this));
				break;
			case Committee_Screen:
				setContentPane(new CommitteeUI(client , this));
				break;
			case Appointment_Screen:
				setContentPane(new AppointmentUI(client , this));
				break;	
			case EmployeesInfo_Screen:
				setContentPane(new EmployeesInfo(client , this));
				break;	
			case RequestsInfo_screen:
				setContentPane(new RequestsInfo(client , this));
				break;	
			case StatisticInfo_screen:
				setContentPane(new StatisticInfo(client , this));
				break;	
			case Assessor_Screen:
				setContentPane(new AssessorUI(client, this));
				break;
			case Chairman_Screen:
				setContentPane(new ChairmanUI(client, this));
				break;
			case Execution_Screen:
				setContentPane(new ExecutionUI(client, this));
				break;
			case Supervisor_Screen:
				setContentPane(new SupervisorUI(client, this));
				break;
			case Testing_Screen:
				setContentPane(new TestingUI(client, this));
				break;
		}
		validate();
		currentScreen = screen;
	}
	
	public User getUser() {
		return user;
	}
}
